/*
 * RootSetSnapshot.java   ver    16 Nov 2009
 * 
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */
package org.mentalsmash.tazio.transactions.objects;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import org.mentalsmash.documentation.Concurrency;

import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;

/**
 * @author Andrea Reale
 * @version 0.0.1
 */

@Concurrency(concurrentAccess = false, specification = "a RootSetSnapshot instance should be accept only by one thread,"
	+ " and that is the thread that owns the Transactions that ons the "
	+ "Private Snapshot. No synchronization is needed.")
class RootSetSnapshot extends SetSnapshot {

    private static final long serialVersionUID = -786807131764196258L;
    private final Set<TransactionalObject> contents;

    /**
     * @param target
     * @param version
     */
    public RootSetSnapshot(final long version) {
	super(RootSet.ROOT_SET_OID, version);
	contents = Sets.newHashSet();
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.Set#add(java.lang.Object)
     */
    @Override
    public boolean add(final Serializable e) {
	Preconditions.checkArgument(e instanceof TransactionalObject,
		"Argument must be a TransactionalObject");
	return contents.add((TransactionalObject) e);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.Set#addAll(java.util.Collection)
     */
    @Override
    public boolean addAll(final Collection<? extends Serializable> c) {
	throw new UnsupportedOperationException();
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.Set#clear()
     */
    @Override
    public void clear() {
	throw new UnsupportedOperationException();

    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.Set#contains(java.lang.Object)
     */
    @Override
    public boolean contains(final Object o) {
	return contents.contains(o);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.Set#containsAll(java.util.Collection)
     */
    @Override
    public boolean containsAll(final Collection<?> c) {
	return contents.containsAll(c);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.Set#isEmpty()
     */
    @Override
    public boolean isEmpty() {
	return contents.isEmpty();
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.Set#iterator()
     */
    @Override
    public Iterator<Serializable> iterator() {
	return new Iterator<Serializable>() {

	    private final Iterator<TransactionalObject> it = contents
		    .iterator();

	    @Override
	    public boolean hasNext() {
		return it.hasNext();
	    }

	    @Override
	    public Serializable next() {
		return it.next();
	    }

	    @Override
	    public void remove() {
		throw new UnsupportedOperationException();

	    }
	};
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.Set#remove(java.lang.Object)
     */
    @Override
    public boolean remove(final Object o) {
	return contents.remove(o);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.Set#removeAll(java.util.Collection)
     */
    @Override
    public boolean removeAll(final Collection<?> c) {
	throw new UnsupportedOperationException();
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.Set#retainAll(java.util.Collection)
     */
    @Override
    public boolean retainAll(final Collection<?> c) {
	throw new UnsupportedOperationException();
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.Set#size()
     */
    @Override
    public int size() {
	return contents.size();
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.Set#toArray()
     */
    @Override
    public Object[] toArray() {
	return contents.toArray();
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.Set#toArray(T[])
     */
    @Override
    public <E> E[] toArray(final E[] a) {
	return contents.toArray(a);
    }

}
